<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>9-3 基于事件委托阻止事件传播机制（推荐）</title>
  <style>
    .outer_box {
      width: 200px;
      height: 200px;
      border: 6px solid lightblue;
      position: relative;
    }

    .inner_box {
      width: 120px;
      height: 120px;
      border: 6px solid lightcoral;
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      margin: auto;
    }

    .box {
      width: 60px;
      height: 60px;
      border: 6px solid lightgreen;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <div class="outer_box">outer_box
    <div class="inner_box">inner_box
      <div class="box">box</div>
    </div>
  </div>

  <script>
    window.onclick = function (ev) {
      let target = ev.target,
        targetName = target.targetName,// 标签名
        targetClassName = target.className;

      if (targetClassName === 'box') {
        console.log('box clicked');
      } else if (targetClassName === 'inner_box') {
        console.log('inner box clicked');
      } else if (targetClassName === 'outer_box') {
        console.log('outer box clicked');
      }
    }
  </script>
</body>

</html>